package jdbclianxi20250325;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行DML语句--INSERT
 */
public class JDBCDemo2 {
    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/mysql?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root"
            );
            //通过连接对象创建语句对象
            Statement statement = connection.createStatement();

            String sql = "INSERT INTO shitou(username,password,nickname,age) " +
                         "VALUES ('吴昊','123456','昊昊',55)";
                  /*
                Statement提供的执行DML语句的方法
                int executeUpdate(String sql)
                执行后返回值表示影响了表中多少条记录
             */
            int num = statement.executeUpdate(sql);

            if(num>0){
                System.out.println("插入成功");
            }
            connection.close();

        } catch (Exception e) {
           e.printStackTrace();
        }
    }
}
